const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() 
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return year + "-" + formatting(month) + "-" + formatting(day-1);
}
function formatting(num){
  if(num < 9){
    var arr=['00','01','02','03','04','05','06','07','08','09'];
    console.log("cur_day",arr[num+1])
    return arr[num+1] 
  }else{
    return num+1
  }
}
function date_format(second){
  var second = Math.floor(second / 1000);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);

  return min + "分钟" + sec + "秒";
} 

function intervalTime(startTime,endTime) {
  // var timestamp=new Date().getTime(); //计算当前时间戳
  // var timestamp = (Date.parse(new Date()))/1000;//计算当前时间戳 (毫秒级)
  //  var date1 = ""; //开始时间
  // if(timestamp<startTime){
  //     date1=startTime;
  // }else{
  //     date1 = timestamp; //开始时间
  // }
  var date1 = startTime //开始时间
  var date2 = endTime //结束时间
  var date3 =  (date2- date1); //时间差的毫秒数
  //计算出相差天数
  var days = Math.floor(date3 / (24 * 3600 * 1000));
  //计算出小时数

  var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));

  //计算相差秒数

  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000);
  console.log(days + "天 " + hours + "小时 ")
  return [days,hours,minutes,seconds]
  // return   days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒"
  // return   days + "天 " + hours + "小时 "
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  date_format: date_format,
  intervalTime:intervalTime,
}
